import {store} from '@/pstore'
import {defineStore} from 'pinia'

export const useTagsViewStroe = defineStore({
    id: 'tagsView',
    state: () => ({
        visitedViews: [],
        cachedViews: []
    }),
    getters: {},
    actions: {
        addView( view) {
            this.addVisitedView(view)
            this.addCachedView(view)      
        },
        addVisitedView( view) {
            if (this.visitedViews.some(v => v.path === view.path)) return
            this.visitedViews.push(
                Object.assign({}, view, {
                    title: view.meta.title || 'no-name'
                })
            )
        },
        addCachedView(view) {
            if (this.cachedViews.includes(view.name)) return
            if (!view.meta.noCache) {
                this.cachedViews.push(view.name)
            }
        },

        delView({dispatch, state}, view) {
            return new Promise(resolve => {
                dispatch('delVisitedView', view)
                dispatch('delCachedView', view)
                resolve({
                    visitedViews: [...state.visitedViews],
                    cachedViews: [...state.cachedViews]
                })
            })
        },
        delVisitedView({commit, state}, view) {
            return new Promise(resolve => {
                for (const [i, v] of state.visitedViews.entries()) {
                    if (v.path === view.path) {
                        state.visitedViews.splice(i, 1)
                        break
                    }
                }
                resolve([...state.visitedViews])
            })
        },
        delCachedView({commit, state}, view) {
            return new Promise(resolve => {
                if (state.cachedViews.includes(view.name)) return
                if (!view.meta.noCache) {
                    state.cachedViews.push(view.name)
                }
                resolve([...state.cachedViews])
            })
        },

        delOthersViews({dispatch, state}, view) {
            return new Promise(resolve => {
                dispatch('delOthersVisitedViews', view)
                dispatch('delOthersCachedViews', view)
                resolve({
                    visitedViews: [...state.visitedViews],
                    cachedViews: [...state.cachedViews]
                })
            })
        },
        delOthersVisitedViews({commit, state}, view) {
            return new Promise(resolve => {
                state.visitedViews = state.visitedViews.filter(v => {
                    return v.meta.affix || v.path === view.path
                })
                resolve([...state.visitedViews])
            })
        },
        delOthersCachedViews({commit, state}, view) {
            return new Promise(resolve => {
                const index = state.cachedViews.indexOf(view.name)
                if (index > -1) {
                    state.cachedViews = state.cachedViews.slice(index, index + 1)
                } else {
                    state.cachedViews = []
                }
                resolve([...state.cachedViews])
            })
        },

        delAllViews({dispatch, state}, view) {
            return new Promise(resolve => {
                dispatch('delAllVisitedViews', view)
                dispatch('delAllCachedViews', view)
                resolve({
                    visitedViews: [...state.visitedViews],
                    cachedViews: [...state.cachedViews]
                })
            })
        },
        delAllVisitedViews( state) {
            return new Promise(resolve => {
                // keep affix tags
                const affixTags = state.visitedViews.filter(tag => tag.meta.affix)
                state.visitedViews = affixTags
                resolve([...state.visitedViews])
            })
        },
        delAllCachedViews(state) {
            return new Promise(resolve => {
                state.cachedViews = []
                resolve([...state.cachedViews])
            })
        },

        updateVisitedView( view) {
            for (let v of state.visitedViews) {
                if (v.path === view.path) {
                    v = Object.assign(v, view)
                    break
                }
            }
        },

        delRightTags(view) {
            return new Promise(resolve => {
                const index = this.visitedViews.findIndex(v => v.path === view.path)
                if (index === -1) {
                    return
                }
                this.visitedViews = this.visitedViews.filter((item, idx) => {
                    if (idx <= index || (item.meta && item.meta.affix)) {
                        return true
                    }
                    const i = this.cachedViews.indexOf(item.name)
                    if (i > -1) {
                        this.cachedViews.splice(i, 1)
                    }
                    return false
                })
                resolve([...this.visitedViews])
            })
        },

        delLeftTags(view) {
            return new Promise(resolve => {
                const index = state.visitedViews.findIndex(v => v.path === view.path)
                if (index === -1) {
                    return
                }
                this.visitedViews = this.visitedViews.filter((item, idx) => {
                    if (idx >= index || (item.meta && item.meta.affix)) {
                        return true
                    }
                    const i = this.cachedViews.indexOf(item.name)
                    if (i > -1) {
                        this.cachedViews.splice(i, 1)
                    }
                    return false
                })
                resolve([...this.visitedViews])
            })
        },
    }
});


export function useTagsViewWithOut(){
    return useTagsViewStroe(store)
}
